#!/usr/bin/env python
# encoding: utf-8
"""
fetchers.py

Created by Bud Gibson on 2010-05-27.
Copyright (c) Bud Gibson 2010. Released under the Artistic/GPL License.
"""

import urllib
import httplib2
try: #buzztracker package is on system path
  from buzztracker.exceptions import HttpError
except ImportError: #buzztracker package is not on system path
  import sys
  import os
  sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
  from buzztracker.exceptions import HttpError

http = httplib2.Http()

baseUrl = 'https://www.googleapis.com/buzz/v1'

def coroutine(func):
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr
    return start

@coroutine
def fetch_buzz_data(processor):
  """A coroutine for fetching data from a buzz endpoint"""
  processed_content = ''
  while True:
    call = (yield processed_content)
    url = baseUrl + call
    response, content = http.request(url, 'GET')
    if response.status == 200:
      processed_content = processor(content)
    else:
      raise HttpError(response.status, response.reason, url)

@coroutine
def fetch_buzz_loop(control_extractor,processor):
  """Takes care of looping for paged results while crawling a buzz network"""
  processed_content = []
  max_results = 100
  fetcher = fetch_buzz_data(control_extractor)
  while True:
    call = (yield processed_content)
    c = 0
    processed_content = []
    total_results = 0
    while True:
      looped_url = call + \
                   '&max-results=' + \
                   str(max_results) + \
                   '&c=' + \
                   str(c)
      data = fetcher.send(looped_url)
      for item in data['entry']:
        processed_content.append(processor(item))
      if data['totalResults']:
        total_results = data['totalResults']
      c = c + max_results
      if c > total_results:
        break
    

def fetch_buzz_test(url):
  """used for testing results sent back by the buzz api"""
  response, content = http.request(url, 'GET')
  print response

def main():
  fetch_buzz_test('http://www.googleapis.com/buzz/v1/people/fpgibson/@groups/@followers?prettyprint=true&alt=json&max-results=100&c=100')

if __name__ == '__main__':
  main()

